home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / jpeglibrary / examples / jpeg.library_save / save_mem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-26  |  3.9 KB  |  156 lines

  1. /* This example use of jpeg.library loads the jpeg file specified
  2.         on the command line and saves a version halved in size to
  3.         'RAM:halved.jpg'. It uses RGB triplet buffer tags to store/supply the
  4.         image data to/from jpeg.library. This example uses memory based jpeg
  5.         streams for the source/destination images.
  6.  
  7.         Does NOT support greyscale jpegs (not implemented in this application
  8.         but jpeg.library supports them).
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include <dos/dos.h>
  14. #include <exec/memory.h>
  15. #include <exec/types.h>
  16.  
  17. #include <clib/dos_protos.h>
  18. #include <clib/exec_protos.h>
  19.  
  20. #include <pragmas/dos_pragmas.h>
  21. #include <pragmas/exec_pragmas.h>
  22.  
  23. #include "jpeg/jpeg.h"
  24. #include "jpeg/jpeg_protos.h"
  25. #include "jpeg/jpeg_pragmas.h"
  26.  
  27. extern struct Library *DOSBase;
  28. struct Library *JpegBase;
  29.  
  30. void main( int argc, char **argv )
  31. {
  32.     JpegBase = OpenLibrary( "jpeg.library", NULL );
  33.     if ( JpegBase != NULL )
  34.     {
  35.         ULONG err;
  36.         struct JPEGDecHandle *jph;
  37.         struct JPEGComHandle *jpc;
  38.         UBYTE *jstream, *cstream;
  39.         ULONG jstreamsize, cstreamsize;
  40.         UBYTE *buffer;
  41.         ULONG x, y;
  42.         BPTR fp, lock, out;
  43.         struct FileInfoBlock *fib;
  44.         UBYTE colourspace;
  45.  
  46.         fib = AllocDosObject( DOS_FIB, TAG_DONE );
  47.         if ( fib != NULL )
  48.         {
  49.             lock = Lock( argv[1], ACCESS_READ );
  50.             if ( lock != NULL )
  51.             {
  52.                 if ( Examine( lock, fib ) )
  53.                 {
  54.                     jstreamsize = fib->fib_Size;
  55.  
  56.                     jstream = AllocVec( jstreamsize, MEMF_PUBLIC | MEMF_CLEAR );
  57.                     if ( jstream != NULL )
  58.                     {
  59.                         fp = OpenFromLock( lock );
  60.                         if ( fp != NULL )
  61.                         {
  62.                             /* Spool file into memory */
  63.                             Read( fp, jstream, jstreamsize );
  64.  
  65.                             Close( fp );
  66.  
  67.                             err = AllocJPEGDecompress( &jph,
  68.                                 JPG_SrcMemStream, jstream,
  69.                                 JPG_SrcMemStreamSize, jstreamsize,
  70.                                 TAG_DONE );
  71.                             if ( !err )
  72.                             {
  73.                                 err = GetJPEGInfo( jph,
  74.                                     JPG_Width, &x, JPG_Height, &y,
  75.                                     JPG_ColourSpace, &colourspace,
  76.                                     JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  77.                                     TAG_DONE );
  78.                                 if ( !err )
  79.                                 {
  80.                                     printf( "colourspace=%d\n", colourspace );
  81.                                     printf( "scaled by half to %dx %d\n", x, y );
  82.  
  83.                                     buffer = AllocRGBFromJPEG( jph,
  84.                                         JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  85.                                         TAG_DONE );
  86.                                     if ( buffer != NULL )
  87.                                     {
  88.                                         err = DecompressJPEG( jph,
  89.                                             JPG_DestRGBBuffer, buffer,
  90.                                             JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  91.                                             TAG_DONE );
  92.                                         if ( !err )
  93.                                         {
  94.                                             err = AllocJPEGCompress( &jpc,
  95.                                                 JPG_DestMemStream, &cstream,
  96.                                                 JPG_DestMemStreamSize, &cstreamsize,
  97.                                                 TAG_DONE );
  98.                                             if ( !err )
  99.                                             {
  100.                                                 err = CompressJPEG( jpc,
  101.                                                     JPG_SrcRGBBuffer, buffer,
  102.                                                     JPG_Width, x, JPG_Height, y,
  103.                                                     TAG_DONE );
  104.                                                 if ( !err )
  105.                                                 {
  106.                                                     out = Open( "ram:halved.jpg", MODE_NEWFILE );
  107.                                                     if ( out != NULL )
  108.                                                     {
  109.                                                         printf( "jpeg saved ok\n" );
  110.  
  111.                                                         Write( out, cstream, cstreamsize );
  112.  
  113.                                                         Close( out );
  114.                                                     }
  115.                                                 }
  116.                                                 else printf( "comrpess jpeg error:%d\n", err );
  117.  
  118.                                                 if ( cstream != NULL ) FreeVec( cstream );
  119.  
  120.                                                 FreeJPEGCompress( jpc );
  121.                                             }
  122.                                             else printf( "alloc jpeg error:%d\n", err );
  123.                                         }
  124.                                         else printf( "descompress jpeg error:%d\n", err );
  125.  
  126.                                         FreeJPEGRGBBuffer( buffer );
  127.                                     }
  128.                                     else printf( "cant allocate rgb buffer\n" );
  129.                                 }
  130.                                 else printf( "get jpeg info error:%d\n", err );
  131.  
  132.                                 FreeJPEGDecompress( jph );
  133.                             }
  134.                             else printf( "alloc jpeg error:%d\n", err );
  135.                         }
  136.                         else printf( "cant open file from lock\n" );
  137.  
  138.                         FreeVec( jstream );
  139.                     }
  140.                     else printf( "cant allocate jstream buffer\n" );
  141.  
  142.                 }
  143.                 else printf( "cant examine file\n" );
  144.  
  145.                 UnLock( lock );
  146.             }
  147.             else printf( "cant open file\n" );
  148.  
  149.             FreeDosObject( DOS_FIB, fib );
  150.         }
  151.         else printf( "cant allocate fib\n" );
  152.     }
  153.  
  154.     if ( JpegBase != NULL ) CloseLibrary( JpegBase );
  155. }
  156.